<?php

class SubscribeController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        /* init menu */
        require_once "models/Db/Event_db.php";
        $eventHandle = new Event_db();

        $pivaList = $eventHandle->getAllPiva();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getUniqueNamebyPiva($piva);
                $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
        }
        $this->view->allDisco = $allDisco;

        foreach ($pivaList as $pivaList2){
            $piva = $pivaList2['piva'];
            $temp = $eventHandle->getPaeseDsicoByPiva($piva);
            $paese[$piva] = $temp['paese'];
        }
        $this->view->paese = array_unique($paese);

        $categoryList = $eventHandle->getAllCategory();
        $this->view->category = $categoryList;

        $zonaList = array('Brescia','Verona','Mantova','Cremona');
        $this->view->zonaList = $zonaList;
    }

    public function indexAction()
    {
        // action body
    }

    public function getSubscribeForm()
    {
        
        $form = new Zend_Form();
        
        $form->setAction("/Subscribe/subscribe-success-disco/id/$id");
        $form->setMethod('get');
        $form->setDescription("Partecipa all'evento");

        
        $form->addElement('text', 'persone');
        $personElement = $form->getElement('persone');
        $personElement->setLabel('Numero di persone:');
        $personElement->setRequired(true)
                               ->addValidator(
                        new Zend_Validate_Digits());

        
        $form->addElement('text', 'tavoli');
        $tavoliElement = $form->getElement('tavoli');
        $tavoliElement->setLabel('Numero di tavoli:');
        $tavoliElement->setRequired(true);

        
        $form->addElement('text', 'bottiglie');
        $bottiglieElement = $form->getElement('bottiglie');
        $bottiglieElement->setLabel('Numero di bottiglie:');
        $bottiglieElement->setRequired(true)
                                ->addValidator(
                        new Zend_Validate_Digits());
        
        $form->addElement('text', 'nome');
        $nomeElement = $form->getElement('nome');
        $nomeElement->setLabel('Nome:');
        $nomeElement->setRequired(true);
        

        $form->addElement('text', 'cognome');
        $cognomeElement = $form->getElement('cognome');
        $cognomeElement->setLabel('Cognome:');
        $cognomeElement->setRequired(true);

        $form->addElement('text', 'mail');
        $mailElement = $form->getElement('mail');
        $mailElement->setLabel('Indirizzo Mail:');
        $mailElement->setRequired(true)
                                ->addValidator(
                        new Zend_Validate_EmailAddress);
        
        $form->addElement('text', 'cell');
        $cellElement = $form->getElement('cell');
        $cellElement->setLabel('Numero di cellulare:');
        $cellElement->setRequired(true)
                                ->addValidator(
                        new Zend_Validate_Digits());

        //Add Captcha
/*
        $captchaElement = new Zend_Form_Element_Captcha('signup', array('captcha' => array( 'captcha' => 'Figlet',
                                                                                            'wordLen' => 6,
                                                                                            'outputWidth' => 70,
                                                                                            'timeout' => 600,))
                                                       );
        $captchaElement->setLabel('Inserisci la parola che vedi qui sotto per poter continuare.');
        $form->addElement($captchaElement);
*/
 
   /*     //hidden value id
        $form->addElement('hidden', 'id');
        $idElement = $form->getElement('id');
        $idElement->setValue($id);
        //print_r($id);
*/
        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Iscriviti');

        return $form;
        
    }

    public function subscribeDiscoEventAction()
    {
        $id=$this->_request->getParam('id');
        //print_r($id);

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();
            $statement="SELECT * FROM edisco WHERE id=".$id;
            $discoList=$db->fetchAll($statement);
            $discoList=$discoList[0];
            
            // i close the connection
            $db->closeConnection();

        }catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
            }
        
        //var_dump($discoList['pren']);
        if($discoList['pren']==0){
            
           $this->_helper->viewRenderer->setNoRender();
           $this->_forward('eventi','index',null);
        }

        
        
        $form=$this->getSubscribeForm();
        
        // send data to the view
        $this->view->dati=$discoList;
        $this->view->form=$form;
    }

    public function subscribeSuccessDiscoAction()
    {
        
        $isErrorForm = 0; // for now i dont have errors on form

        $id=$this->_request->getParam('id');
        //echo "id:" . $id;
        $form = $this->getSubscribeForm();
        
        
        if ($form->isValid($_GET)) {
            // dati in input
            $persone=$this->_request->getParam('persone');
            $bottiglie=$this->_request->getParam('bottiglie');
            $tavoli=$this->_request->getParam('tavoli');
            $id_evento=$this->_request->getParam('id');
            $mail=$this->_request->getParam('mail');
            $nome=$this->_request->getParam('nome');
            $cognome=$this->_request->getParam('cognome');
            $cell=$this->_request->getParam('cell');
            //echo "valid";
        }
        else{
            $isErrorForm = 1;
            //echo "invalid";
        }
        
        //echo "<pre>";
        //print_r($this->_request->getParams());
        //echo "</pre>";
        if($isErrorForm==0){ // if the form is valid
            $prenotazione = array(
                                "state" => "0",
                                "n_table" => $tavoli,
                                "n_bottle" => $bottiglie,
                                "id_event" => $id_evento,
                                "npersone" => $persone,
                                'email' => $mail,
                                'nome' => $nome,
                                'cognome' => $cognome,
                                'cell' => $cell
                                );
            
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $db->insert('pdisco',$prenotazione);

                // i close the connection
                $db->closeConnection();

            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }

            $this->_helper->viewRenderer->setNoRender();

            $this->_forward('smtp-send-mail', 'email', null, array( 'email' => $mail,
                                                                    'n_table' => $tavoli,
                                                                    'n_bottle' => $bottiglie,
                                                                    'id_event' => $id_evento,
                                                                    'nome' => $nome,
                                                                    'cognome' => $cognome,
                                                                    'cell' => $cell,
                                                                    'npersone' => $persone )
                            );
            
                            } 
        else{ // i have errors
            $this->view->form = $form;
         }
         
         
        $this->view->isErrorForm = $isErrorForm;

         
         
    }

    public function validateSuccessDiscoAction()
    {
        // action body
        //Prendo la email 
        $emailToValidate = $this->_request->getQuery("email");
        $eventToValidate = $this->_request->getQuery("id_event");
        $tableToValidate = $this->_request->getQuery("numero_tavoli");
        //print_r($emailToValidate);
        //print_r($eventToValidate);

        try{
            require_once "models/Db/Db_Db.php";

            $db=Db_Db::conn();

            //Controllo se l'utente e l'evento sono nel database
            $statement = "SELECT COUNT(id) AS total From pdisco 
                WHERE email='".$emailToValidate."' AND state=0 AND id_event='".$eventToValidate."'"." AND numero_tavoli='".$tableToValidate."'";
            //print_r($statement);
            
            $results = $db->fetchOne($statement);
            //print_r("\nresult=" . $results);
            //se ottengo max una riga, allora l'email è valida
            if($results < 4){ // 4 is the magic number
                $conditions[]="email='$emailToValidate'";
                $conditions[]="id_event='$eventToValidate'";
                $conditions[]="numero_tavoli='$tableToValidate'";
                //print_r($conditions);

                //Updates
                $updates = array('state' => 1);
                //print_r($updates);

                $results = $db->update('pdisco',$updates, $conditions);
                //print_r("\nresult2=" . $results);
                if($results < 4){
                    $this->view->activated = true;
                } else {
                    $this->view->activated = false;
                }
                // i close the connection
                $db->closeConnection();
            } else {
                $this->view->activated = false;
                }

        } catch(Zend_Db_Exception $e) {

            echo $e->getMessage();
        
    }
    
    }

    public function getDiscoListForm($id)
    {
        
        $form = new Zend_Form();
        
        $form->setAction("/Subscribe/subscribe-disco-list-success/id/$id");
        $form->setMethod('get');
        $form->setDescription("Iscriviti alla lista");
        
        $form->addElement('text', 'nome');
        $nomeElement = $form->getElement('nome');
        $nomeElement->setLabel('Nome:');
        $nomeElement->setRequired(true);
        
        $form->addElement('text', 'numero_persone');
        $numeroPersoneElement = $form->getElement('numero_persone');
        $numeroPersoneElement->setLabel('Numero persone:');
        $numeroPersoneElement->setRequired(true)
                   ->addValidator(
                        new Zend_Validate_Digits());

        
        $form->addElement('text', 'email');
        $emailElement = $form->getElement('email');
        $emailElement->setLabel('Email:');
        $emailElement->setRequired(true)
                //Add Validator
                ->addValidator(
                    new Zend_Validate_EmailAddress()
               );
        //Add Filter
        $emailElement->addFilter(new Zend_Filter_StripTags());

        
        $form->addElement('text', 'phone');
        $phoneElement = $form->getElement('phone');
        $phoneElement->setLabel('Numero cellulare:');
        $phoneElement->setRequired(true)
                ->addValidator(
                        new Zend_Validate_Digits());
        
        
        $form->addElement('textarea', 'note');
        $noteElement = $form->getElement('note');
        $noteElement->setLabel('Note: (Facoltativo)')
                    ->setAttrib('id', 'tagedit')
                    ->setAttrib('COLS','30')
                    ->setAttrib('ROWS','10');

        
        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Iscriviti');
        
        return $form;

    }

    public function getDiscoTableForm($id){
        
        $form = new Zend_Form();
        
        $form->setAction("/Subscribe/subscribe-disco-table-success/id/$id");
        $form->setMethod('get');
        $form->setDescription("Prenota dei tavoli");
        
        $form->addElement('text', 'nome');
        $nomeElement = $form->getElement('nome');
        $nomeElement->setLabel('Nome:');
        $nomeElement->setRequired(true);
        
        $form->addElement('text', 'numero_persone');
        $numeroPersoneElement = $form->getElement('numero_persone');
        $numeroPersoneElement->setLabel('Numero persone:');
        $numeroPersoneElement->setRequired(true)
                   ->addValidator(
                        new Zend_Validate_Digits());

                $form->addElement('text', 'numero_tavoli');
        $numeroTavoliElement = $form->getElement('numero_tavoli');
        $numeroTavoliElement->setLabel('Numero tavoli:');
        $numeroTavoliElement->setRequired(true)
                   ->addValidator(
                        new Zend_Validate_Digits());
        
        $form->addElement('text', 'numero_bottiglie');
        $numeroBottiglieElement = $form->getElement('numero_bottiglie');
        $numeroBottiglieElement->setLabel('Numero bottiglie:');
        $numeroBottiglieElement->setRequired(true)
                   ->addValidator(
                        new Zend_Validate_Digits());

        $form->addElement('text', 'email');
        $emailElement = $form->getElement('email');
        $emailElement->setLabel('Email:');
        $emailElement->setRequired(true)
                //Add Validator
                ->addValidator(
                    new Zend_Validate_EmailAddress()
               );
        //Add Filter
        $emailElement->addFilter(new Zend_Filter_StripTags());

        
        $form->addElement('text', 'phone');
        $phoneElement = $form->getElement('phone');
        $phoneElement->setLabel('Numero cellulare:');
        $phoneElement->setRequired(true)
                ->addValidator(
                        new Zend_Validate_Digits());
        
        
         $form->addElement('textarea', 'note');
        $noteElement = $form->getElement('note');
        $noteElement->setLabel('Note: (Facoltativo)')
        //$noteElement->setAttrib('id', 'tagedit');
                    ->setAttrib('COLS','30')
                    ->setAttrib('ROWS','10');
        
        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Prenota');
        
        return $form;
        
    }
    
     public function getDiscoInfoForm($id)
    {
        
        $form = new Zend_Form();
        
        $form->setAction("/Subscribe/subscribe-disco-info-success/id/$id");
        $form->setMethod('get');
        $form->setDescription("Richiedi informazioni su whichclub, sarai contattato dai nostri amministratori:");
        
        $form->addElement('text', 'nome');
        $nomeElement = $form->getElement('nome');
        $nomeElement->setLabel('Nome:');
        $nomeElement->setRequired(true);
        
        $form->addElement('text', 'email');
        $emailElement = $form->getElement('email');
        $emailElement->setLabel('Email:');
        $emailElement->setRequired(true)
                //Add Validator
                ->addValidator(
                    new Zend_Validate_EmailAddress()
               );
        //Add Filter
        $emailElement->addFilter(new Zend_Filter_StripTags());

        
        $form->addElement('text', 'phone');
        $phoneElement = $form->getElement('phone');
        $phoneElement->setLabel('Numero cellulare:');
        $phoneElement->setRequired(true)
                ->addValidator(
                        new Zend_Validate_Digits());
        
        
        $form->addElement('textarea', 'note');
        $noteElement = $form->getElement('note');
        $noteElement->setLabel('Note: (Facoltativo)')
        //$noteElement->setAttrib('id', 'tagedit');
                            ->setAttrib('COLS','30')
                            ->setAttrib('ROWS','10');
        //
        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Invia');
        
        return $form;

    }

    
    /**
     * Azione per iscriversi alla lista di un evento
     *
     *
     *
     */
    public function subscribeDiscoListAction()
    {
        $id=$this->_request->getParam('id','err');
        //echo "id: ". $id;
        $form=$this->getDiscoListForm($id);
        $this->view->form=$form;
        
    }

    /**
     * Azione per ottenere informazioni su un evento
     *
     *
     *
     */
    public function subscribeDiscoInfoAction()
    {
        $id=$this->_request->getParam('id','err');
        //echo "id: ". $id;
        $form=$this->getDiscoInfoForm($id);
        
        $this->view->form=$form; 
    }

    /**
     * Azione per prenotare tavoli per un evento
     *
     *
     *
     */
    public function subscribeDiscoTableAction()
    {
        $id=$this->_request->getParam('id','err');
        //echo "id: ". $id;
        $form=$this->getDiscoTableForm($id);
        $this->view->form=$form;
        
    }

    public function subscribeDiscoTableSuccessAction()
    {
        $isErrorForm = 0; // for now i dont have errors on form

        $id=$this->_request->getParam('id','err');
        $form = $this->getDiscoTableForm($id);
        
        
        if ($form->isValid($_GET)) {
            // dati in input
            $nome=$this->_request->getParam('nome');
            $numeroPersone=$this->_request->getParam('numero_persone');
            $email=$this->_request->getParam('email');
            $phone=$this->_request->getParam('phone');
            $note=$this->_request->getParam('note');
            $numero_bottiglie=$this->_request->getParam('numero_bottiglie');
            $numero_tavoli=$this->_request->getParam('numero_tavoli');
        }
        else{
            $isErrorForm = 1;
        }
        
        //echo "<pre>";
        //print_r($this->_request->getParams());
        //echo "</pre>";
        if($isErrorForm==0){ // if the form is valid
            $prenotazione = array(
                                "state" => "0",
                                "id_event" => $id,
                                "nome" => $nome,
                                "numero_persone" => $numeroPersone,
                                "note" => $note,
                                'email' => $email,
                                'phone' => $phone,
                                'numero_bottiglie' => $numero_bottiglie,
                                'numero_tavoli' => $numero_tavoli
                                );
            
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $db->insert('pdisco',$prenotazione);

                // i close the connection
                $db->closeConnection();

            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }

            //$this->_helper->viewRenderer->setNoRender();
            $this->_forward('smtp-send-mail', 'email', null, array( 'email' => $email,
                                                                    "id_event" => $id,
                                                                    "nome" => $nome,
                                                                    "numero_persone" => $numeroPersone,
                                                                    "note" => $note,
                                                                    'email' => $email,
                                                                    'phone' => $phone,
                                                                    'numero_bottiglie' => $numero_bottiglie,
                                                                    'numero_tavoli' => $numero_tavoli)                                
                                                                   );
       }
      
        else{ // i have errors
            $this->view->form = $form;
         }
        $this->view->isErrorForm = $isErrorForm;
    }

    public function subscribeDiscoInfoSuccessAction()
    {
        $isErrorForm = 0; // for now i dont have errors on form

        $id=$this->_request->getParam('id','err');
        $form = $this->getDiscoInfoForm($id);
        
        
        if ($form->isValid($_GET)) {
            // dati in input
            $nome=$this->_request->getParam('nome');
            $email=$this->_request->getParam('email');
            $phone=$this->_request->getParam('phone');
            $note=$this->_request->getParam('note');
        }
        else{
            $isErrorForm = 1;
            //echo "invalid";
        }
        
        //  data odierna
        $today=date('y-m-d');
        
        if($isErrorForm==0){ // if the form is valid
            
            $info = array(
                                "nome" => $nome,
                                "note" => $note,
                                'email' => $email,
                                'phone' => $phone,
                                'data' => $today,
								'id_evento' => $id,
                                );
            
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $db->insert('informazioni',$info);

                // i close the connection
                $db->closeConnection();

            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }

        } // write data
        else{ // i have errors
            $this->view->form = $form;
        }

        
        $this->view->isErrorForm = $isErrorForm;

    }

    // manca la view e la validazione
    public function subscribeDiscoListSuccessAction()
    {
        $isErrorForm = 0; // for now i dont have errors on form

        $id=$this->_request->getParam('id','err');
        //echo "id: " . $id;
        $form = $this->getDiscoListForm($id);
        
        
        if ($form->isValid($_GET)) {
            // dati in input
            $nome=$this->_request->getParam('nome');
            $numeroPersone=$this->_request->getParam('numero_persone');
            $email=$this->_request->getParam('email');
            $phone=$this->_request->getParam('phone');
            $note=$this->_request->getParam('note');
        }
        else{
            $isErrorForm = 1;
            //echo "invalid";
        }
        
        //echo "<pre>";
        //print_r($this->_request->getParams());
        //echo "</pre>";
        if($isErrorForm==0){ // if the form is valid
            $prenotazione = array(
                                "state" => "0",
                                "id_event" => $id,
                                "nome" => $nome,
                                "numero_persone" => $numeroPersone,
                                "note" => $note,
                                'email' => $email,
                                'phone' => $phone,
                                'numero_tavoli' => 0,
                                'numero_bottiglie' => 0
                                );
            
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $db->insert('pdisco',$prenotazione);

                // i close the connection
                $db->closeConnection();

            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }

            //$this->_helper->viewRenderer->setNoRender();
            $this->_forward('smtp-send-mail', 'email', null, array( 'email' => $email,
         
                                                                    "id_event" => $id,
                                                                    "nome" => $nome,
                                                                    "numero_persone" => $numeroPersone,
                                                                    "note" => $note,
                                                                    'email' => $email,
                                                                    'phone' => $phone )                                
                                                                   );
       }
        else{ // i have errors
            $this->view->form = $form;
         }
        $this->view->isErrorForm = $isErrorForm;
    }
}